//
//  GestureUIImageView.m
//  UI05_触摸_手势
//
//  Created by aaaaaa on 16/6/12.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "GestureUIImageView.h"

@implementation GestureUIImageView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithImage:(UIImage *)image withFrame:(CGRect)frame{
    self = [super initWithImage:image];
    if (self) {
        
        //userInteractionEnabled是UIView属性，控制视图是否响应触摸 UILable和UIImageView是默认关闭响应的
        self.userInteractionEnabled = YES;
        
        self.frame = frame;
        [self addTap];
        [self addSwip];
        [self addLongPress];
        [self addScreenEdge];
        [self addPinch];
        [self addPan];
        [self addRotation];
    }
    
    return self;
}

/**点击手势*/
- (void)addTap{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    
    /*** 设置几个手指点击，默认一个**/
    tap.numberOfTouchesRequired = 2;
    
    /**设置点击几下 默认一下**/
    tap.numberOfTapsRequired = 2;
    
    [self addGestureRecognizer:tap];
    [tap release];
}

- (void)tapAction:(UITapGestureRecognizer *)tap {
    NSLog(@"%d, %s", __LINE__, __FUNCTION__);
}

/*轻扫手势*/
- (void)addSwip {
    UISwipeGestureRecognizer *swip = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipAction:)];
    
    swip.direction = UISwipeGestureRecognizerDirectionDown;
    
    [self addGestureRecognizer:swip];
    [swip release];
}

- (void)swipAction:(UISwipeGestureRecognizer *)swip{
    NSLog(@"%d, %s", __LINE__, __FUNCTION__);
    
    
}

/*长按手势*/
- (void)addLongPress{
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
    [self addGestureRecognizer:longPress];
    [longPress release];
}

- (void)longPressAction:(UILongPressGestureRecognizer *)longPress{
    NSLog(@"%d, %s", __LINE__, __FUNCTION__);
}


/*屏幕边缘*/
- (void)addScreenEdge{
    UIScreenEdgePanGestureRecognizer *screenEdge = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(screenEdgeAction:)];
    screenEdge.edges = UIRectEdgeRight;
    [self addGestureRecognizer:screenEdge];
    [screenEdge release];
}

- (void)screenEdgeAction:(UIScreenEdgePanGestureRecognizer *)screenEdge{
    NSLog(@"%d, %s", __LINE__, __FUNCTION__);
}

/*捏合手势*/
- (void)addPinch{
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
    [self addGestureRecognizer:pinch];
    [pinch release];
}

- (void)pinchAction:(UIPinchGestureRecognizer *)pinch{
    NSLog(@"%d, %s", __LINE__, __FUNCTION__);
    /*transfrom 属性：可以改变视图呈现的方式，例如旋转、缩放、拖拽*/
    self.transform = CGAffineTransformScale(self.transform, pinch.scale, pinch.scale);
    //重置
    pinch.scale = 1.0;
}

/*拖拽手势*/
- (void)addPan{
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
    [self addGestureRecognizer:pan];
    [pan release];
}

- (void)panAction:(UIPanGestureRecognizer *)pan{
    NSLog(@"%d, %s", __LINE__, __FUNCTION__);
    
    CGPoint point = [pan translationInView:self];
    self.transform = CGAffineTransformTranslate(self.transform, point.x, point.y);
    
    //重置
    [pan setTranslation:CGPointZero inView:self];
}

/*旋转手势*/
- (void)addRotation {
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
    [self addGestureRecognizer:rotation];
    [rotation release];
}

- (void)rotationAction:(UIRotationGestureRecognizer *)rotation{
    NSLog(@"%d, %s", __LINE__, __FUNCTION__);
    
    self.transform = CGAffineTransformRotate(self.transform, rotation.rotation);
    
    //重置
    rotation.rotation = 0.0;
}






@end
